/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.vce;

import com.vce.election.domain.Town;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.codec.digest.DigestUtils;


/**
 *
 * @author Ggutierrez
 */
public class User implements Persistent {
    private Long id;
    private String firstName;
    private String lastName;
    private String login;
    private String password;
    private char role = 'O';
    private Set<Town> assignedTowns = new HashSet<Town>();

    public User() {
    }

    public User(String firstName, String lastName, String login, String password, char role) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.login = login;
        this.password = password;
        this.role = role;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    public boolean checkPassword(String raw){
//        return this.password != null && this.password.equals(DigestUtils.md5Hex(raw));
        return this.password != null && this.password.equals(raw);
    }
    
    public void setUnencryptedPassword(String raw){
        this.password = DigestUtils.md5Hex(raw);
    }
    
    @Override
    public int hashCode(){
        return login.hashCode();
    }
    
    @Override
    public boolean equals(Object other){
        if(other == this){
            return true;
        }
        if(!(other instanceof User)){
            return false;
        }
        User _o = (User)other;
        return login.equals(_o.login);
    }

    public char getRole() {
        return role;
    }

    public void setRole(char role) {
        this.role = role;
    }

    public Set<Town> getAssignedTowns() {
        return assignedTowns;
    }

    public void setAssignedTowns(Set<Town> assignedTowns) {
        this.assignedTowns = assignedTowns;
    }
    
    public boolean isAdministrator(){
        return this.role == 'A';
    }
    
    public boolean isSupervisor(){
        return this.role == 'S';
    }
    
    public boolean isOperator(){
        return this.role == 'O';
    }
    
    public String getNameRole(){
        if(role == 'A'){
            return "Administrador";
        }else if (role == 'O'){
            return "Operador";
        }else if (role == 'S'){
            return "Supervisor";
        }else{
            return "Desconocido";
        }
    }
}
